#include "StdAfx.h"
#include "ModelIndex.h"
#include "AbstractItemModel.h"

namespace DuiLib
{


	CModelIndex::CModelIndex()
		:_row(-1), _column(-1), _interPtr(nullptr), _model(nullptr)
	{
	}

	CModelIndex::CModelIndex(int row, int col, const void* interPtr, const CAbstractItemModel* model)
		: _row(row), _column(col), _interPtr(interPtr), _model(model)
	{
	
	}

	CModelIndex::~CModelIndex()
	{
	}

	bool CModelIndex::isValid() const
	{
		return _row >= 0 && _column >= 0 && _model != nullptr;
	}

	int CModelIndex::row() const
	{
		return _row;
	}

	int CModelIndex::column() const
	{
		return _column;
	}

	const CAbstractItemModel* CModelIndex::model() const
	{
		return _model;
	}
	
	const void* CModelIndex::interPtr() const
	{
		return _interPtr;
	}
	
	CModelIndex CModelIndex::parent() const
	{
		if (!model())
		{
			return CModelIndex();
		}

		return model()->parent(*this);

	}

	CModelIndex CModelIndex::sibling(int row, int col) const
	{
		if (!model())
		{
			return CModelIndex();
		}

		if (row == _row && col == _column)
		{
			return *this;
		}

		return model()->sibling(row, col, *this);
	}

	int CModelIndex::childCount() const
	{
		if (!model())
		{
			return 0;
		}

		return model()->rowCount(*this);
	}

	CModelIndex CModelIndex::child(int row, int col) const
	{
		if (!model())
		{
			return CModelIndex();
		}

		return model()->index(row, col, *this);
	}

	CVar CModelIndex::data() const
	{
		if (!model())
		{
			return CVar();
		}

		return model()->data(*this);
	}

	bool CModelIndex::operator ==(const CModelIndex& index) const
	{

		return _row == index.row() &&
				_column == index.column() &&
				_interPtr == index.interPtr() &&
				_model == index.model();
	}

	bool CModelIndex::operator !=(const CModelIndex& index) const
	{
		return !(*this == index);
	}

	bool CModelIndex::operator <(const CModelIndex& index) const
	{
		return  _row < index.row() ||
				(_row == index.row() && (_column < index.column() ||
				(_column == index.column() && (_interPtr < index.interPtr() ||
				(_interPtr == index.interPtr() && _model < index.model())))));
	}


}